var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nameCommunity: "",
    communityAreaNameList: [],
    communityAreaIdList:[],
    communityAreaName: "请选择",
    communityAreaId:0,
    communityArea_pickerDisabled: true,

    communityFloorNameList: [],
    communityFloorIdList:[],
    communityFloorName: "请选择",
    communityFloorId: 0,
    communityFloor_pickerDisabled: true,

    communityRoomNameList: [],
    communityRoomIdList: [],
    communityRoomName: "请选择",
    communityRoomId: 0,
    communityRoom_pickerDisabled: true,
    
    mrRoomFlag:0,//常住房间
    identityType:0,//身份属性
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    //检测是否存在 当前小区
    if (!wx.getStorageSync('nameCommunity')){
      Common.showToast("请选择所在小区，再操作");
      setTimeout(function () {
        wx.navigateBack()
      }, 1000);
      return false;
    }


    that.setData({
      nameCommunity: wx.getStorageSync('nameCommunity')
    });
    var url = app.globalData.domain + "/client/community/getAreaByCommunity";
    var parm = {
      idCommunity: wx.getStorageSync('idCommunity'),
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取小区的区域=>", res);
      var communityAreaNameList = [];
      var communityAreaIdList = [];
      var communityArea_pickerDisabled = true;
      if (res.data.length > 0){
        for (var index in res.data) {
          communityAreaNameList[index] = res.data[index].name;
          communityAreaIdList[index] = res.data[index].id;
        }
        communityArea_pickerDisabled = false;
      }
      that.setData({
        communityAreaNameList: communityAreaNameList,
        communityAreaIdList: communityAreaIdList,
        communityArea_pickerDisabled: communityArea_pickerDisabled
      });
      console.log("communityList", communityList);
    }, error => {
      Common.showToast(error);
    });
  },
  //选择小区域
  communityAreaListChange: function (event){
    var that = this;
    console.log("communityListValue", event);
    that.setData({
      communityAreaName: that.data.communityAreaNameList[event.detail.value],
      communityAreaId: that.data.communityAreaIdList[event.detail.value]
    });
    var url = app.globalData.domain + "/client/community/getFloorByArea";
    var parm = {
      idArea: that.data.communityAreaId,
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取区域的楼宇=>", res);
      var communityFloorNameList = [];
      var communityFloorIdList = [];
      var communityFloor_pickerDisabled = true;
      if (res.data.length > 0) {
        for (var index in res.data) {
          communityFloorNameList[index] = res.data[index].name;
          communityFloorIdList[index] = res.data[index].id;
          console.log("communityFloorNameList", index);
        }
        communityFloor_pickerDisabled = false;
      }
      that.setData({
        communityFloorNameList: communityFloorNameList,
        communityFloorIdList: communityFloorIdList,
        communityFloor_pickerDisabled: communityFloor_pickerDisabled
      });
      console.log("communityFloorNameList", communityFloorNameList);
    }, error => {
      Common.showToast(error);
    });
  },
  //选择栋
  communityFloorListChange: function (event) {
    var that = this;
    console.log("communityListValue", event);

    that.setData({
      communityFloorName: that.data.communityFloorNameList[event.detail.value],
      communityFloorId: that.data.communityFloorIdList[event.detail.value]
    });
    var url = app.globalData.domain + "/client/community/getRoomByFloor";
    var parm = {
      idFloor: that.data.communityFloorId,
      token: wx.getStorageSync("token")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取楼宇的房屋=>", res);
      var communityRoomNameList = [];
      var communityRoomIdList = [];
      var communityRoom_pickerDisabled = true;
      if (res.data.length > 0) {
        for (var index in res.data) {
          communityRoomNameList[index] = res.data[index].name;
          communityRoomIdList[index] = res.data[index].id;
          console.log("communityRoomNameList", index);
        }
        communityRoom_pickerDisabled = false;
      }
      that.setData({
        communityRoomNameList: communityRoomNameList,
        communityRoomIdList: communityRoomIdList,
        communityRoom_pickerDisabled: communityRoom_pickerDisabled
      });
      console.log("communityRoomNameList", communityRoomNameList);
    }, error => {
      Common.showToast(error);
    });
  },
  //选择室
  communityRoomListChange: function (event) {
    var that = this;
    console.log("communityListValue", event);
    that.setData({
      communityRoomName: that.data.communityRoomNameList[event.detail.value],
      communityRoomId: that.data.communityRoomIdList[event.detail.value]
    });
  },
  //设置默认
  setMrRoom: function (event){
    if (event.detail.value){
        this.setData({
          mrRoomFlag:1
        });
    }else{
      this.setData({
        mrRoomFlag: 0
      });
    }
  },
  //身份属性
  identityTypeChange:function(event){
    this.setData({
      identityType: event.detail.value
    });
  },
  //提交表单，绑定房屋
  submitBindRoomForm:function(){
    var that = this;
    var url = app.globalData.domain + "/client/community/bindRoom";
    if (that.data.communityRoomId == 0){
      Common.showToast("请选择区域");
      return false;
    }

    var parm = {
      idRoom: that.data.communityRoomId,
      token: wx.getStorageSync("token"),
      type: that.data.identityType, 
      mr: that.data.mrRoomFlag
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("提交表单，绑定房屋=>", res);
      Common.showToast("绑定成功");
      setTimeout(function () {
        wx.navigateBack();
      }, 1500);
    }, error => {
      Common.showToast(error);
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})